﻿using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class HuiyuanDAL
    {
        
        public HuiyuanDAL(MyContext myContext)
        {
            MyContext = myContext;
        }
        /// <summary>
        /// 添加
        /// </summary>
        public MyContext MyContext { get; }
        public int Add(Huiyuan huiyuan)
        {
            huiyuan.Pwd = huiyuan.Pwd.GetMD5();
            MyContext.Add(huiyuan);
            return MyContext.SaveChanges();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int Login(string username, string password)
        {
            var n = MyContext.huiyuans.FirstOrDefault(x => x.UserName == username);
            if (n==null)
            {
                return 0;
            }
            else
            {
                if(n.Pwd==password.GetMD5())
                {
                    return n.Id;
                }
                else
                {
                    return -1;
                }
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<Huiyuan> GetHuiyuans()
        {
            return MyContext.huiyuans.ToList();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int  Delete(int id)
        {
            var del = MyContext.huiyuans.FirstOrDefault(x => x.Id == id);
            if (del != null)
            {
                MyContext.huiyuans.Remove(del);
            }
            return MyContext.SaveChanges(); 
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="huiyuan"></param>
        /// <returns></returns>
        public int Update(Huiyuan huiyuan)
        {
            var Upd = MyContext.huiyuans.Find(huiyuan.Id);
            if(Upd != null)
            {
                Upd.TuPian = huiyuan.TuPian;
                Upd.Beizhu = huiyuan.Beizhu;
                Upd.Name = huiyuan.Name;
                Upd.UserName = huiyuan.UserName;
                Upd.Pwd = huiyuan.Pwd;
                Upd.HuiYuanDj = huiyuan.HuiYuanDj;
                Upd.Shengre = huiyuan.Shengre;
                Upd.Phone = huiyuan.Phone;
                Upd.Money = huiyuan.Money;
                Upd.Date = huiyuan.Date;
                Upd.FeiYong = huiyuan.FeiYong;
                Upd.Leiji = huiyuan.Leiji;
            }
            return MyContext.SaveChanges();
        }
        /// <summary>
        /// 反添
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Huiyuan GetHuiyuan(int id)
        {
            return MyContext.huiyuans.Find(id);
        }
    }
}
